﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace socketTest1.Client
{
    public partial class UserMain : Form
    {
        #region

        private UserInfo userMode;
        private string hostName = "127.0.0.1";
        public UserInfo UserMode
        {
            get { return userMode; }
            set { userMode = value; }
        }

        #endregion

        public UserMain()
        {
            InitializeComponent();
        }

        private void UserMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("你确定要关闭窗口吗,关闭此窗口将退出整个程序！", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

            if (result == DialogResult.OK)
            {
                Application.ExitThread();
            }
            else
            {
                e.Cancel = true;//取消关闭
            }
        }

        private void UserMain_Load(object sender, EventArgs e)
        {


            timer_loadUserList.Enabled = true;
            timer_loadAppInfo.Enabled = true;
        }

        public void Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            try
            {
                socket.Connect("127.0.0.1", 2000);
            }
            catch (Exception se)
            {
                MessageBox.Show("连接错误" + se.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information);
            }
            //发送给远程主机的请求内容串 
            string sendStr = "GET / HTTP/1.1\r\nHost: " + hostName + "\r\nConnection: Close\r\n\r\n";
            //创建bytes字节数组以转换发送串 
            byte[] bytesSendStr = new byte[1024];
            //将发送内容字符串转换成字节byte数组 
            bytesSendStr = Encoding.ASCII.GetBytes(sendStr);

            try
            {
                //向主机发送请求 
                socket.Send(bytesSendStr, bytesSendStr.Length, 0);
            }
            catch (Exception ce)
            {
                MessageBox.Show("发送错误:" + ce.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information);
            }
        }

        private void LoginServer()
        {
            try
            {
            }
            catch
            {
                MessageBox.Show("登录服务器失败,请稍后重试！");
            }
        }

        private void timer_loadUserList_Tick(object sender, EventArgs e)
        {
            foreach (TreeNode tn in treeView1.Nodes)
            {
                foreach (TreeNode tnc in tn.Nodes)
                {
                    tnc.ContextMenuStrip = contextMenuStrip1;
                }
            }
            timer_loadUserList.Enabled = false;
        }

        private void timer_loadAppInfo_Tick(object sender, EventArgs e)
        {

        }
    }
}
